<template>
    <BasicModal v-bind="$attrs" @register="userRegister" :title="getTitle" width="900px" @ok="submitAudit">
        <BasicTable @register="registerDevTable" :loading="loading" :rowSelection="{ type: 'checkbox' }">
        </BasicTable>
    </BasicModal>
</template>

<script lang="ts" setup>
import { BasicModal, useModalInner } from '/@/components/Modal';
import { ref, reactive } from 'vue';
import { useListPage } from '/@/hooks/system/useListPage'
import { devAndUteList } from '../LabCheckItemsList.api';
import { devAndUteColumns } from '../LabCheckItemsList.data';
import { BasicTable } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';


const loading = ref(false);
const getTitle = ref('设备器具');
const formData = reactive({
    mId: "",
    devName:"",
});

const emits = defineEmits<{
    (event: "getSelectResult", devList: Array<any>): void;
}>();

const [userRegister, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({
        minHeight: 180,
    });
    formData.mId = data.mId;
    formData.devName = data.devName;
    loading.value = true;
});
const searchFormSchema: FormSchema[] = [
    {
      label: '设备名称',
      field: "formData.devName",
      component: 'Input',
      colProps: { span: 6 },
    },
  ];
//注册table数据
const { tableContext } = useListPage({
    tableProps: {
        api: devAndUteList,
        columns: devAndUteColumns,
        canResize: false,
        formConfig: {
        schemas: searchFormSchema,
        labelWidth: 70,
        },
        showIndexColumn: true,
        showTableSetting: false,
        showActionColumn: false,
        beforeFetch: (params) => {
            if(params.formData){
                params.devName2 = params.formData.devName;
                return Object.assign(params, { mId: formData.mId,devName:params.devName2 });
            }else {
                return Object.assign(params, { mId: formData.mId });
            };  
        },
    },
})


const [registerDevTable, { getSelectRows }] = tableContext;

async function submitAudit() {
    const selectedRows = getSelectRows();
    const devList = [{
        devId: '',
        devName: '',
        devNum: '',
        devValidity: '',
        devModel:'',
    }];
    if (selectedRows.length > 0) {
        devList.splice(0, devList.length);
        selectedRows.forEach(item => {
            devList.push({
                devId: item.id,
                devName: item.devName,
                devNum: item.devNumber,
                devValidity: item.endCheckDate,
                devModel:item.devModel,
            })
        });
    }
    emits('getSelectResult', devList);
    closeModal();

}


</script>